/**
 * 定义监测仪器管理模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.WasteWater.DataComparison.dataComparisonDetailManager', ['water.entity.dataComparison','water.common.dateUtil',]);
	
	module.controller('fsdataComparisonManagerDetailController',function($rootScope,$scope,$filter,$dataComparisonService,$controller,$monitorStationService,$dateUtil,$state,$stateParams){     
		/**
		* 报告详情切换事件
		*/
		$scope.onDetailClick;
		/**
		 * 报告详情页面保存事件
		 */
		$scope.onSaveClick;		
		/**
		 * 添加table行事件
		 */
		$scope.onAddTableRowClick;	
		/**
		 * 获取在线监测数据事件
		 */
		$scope.onGetMonitorDataMinClick;
		/**
		 * 标样批号
		 */
		$scope.STASAMPLEBATCHNUMBER;
		/**
		 * 删除table行事件
		 */
		$scope.onDetTableRowClick;
		/**
		 * 误差计算事件
		 */
		$scope.onCalculateErrorClick;
		/**
		 * 数据比对信息
		 */
		 $scope.essential={};
		/**
		 * 查询条件参数集
		 */
		$scope.queryObject={};
		/**
		 * 监测参数数组，用于详情页面监测项单选
		 */
		$scope.parmData=[];
		/**
		 * 验收比对报告
		 */
		$scope.TestingReportData=[{"k":0,"v":"报告预览"},
		      		        	{"k":1,"v":"基本信息"},{"k":2,"v":"实际水样测定"},{"k":3,"v":"质控样品测定"},{"k":4,"v":"技术说明"}];
		
		/**
		 * 报告类型
		 */
		$scope.reportTypeDta=[{"k":1,"v":"测试报告"},{"k":2,"v":"比对监测情况"}];
		/**
		 * 监测参数编码
		 */
		var parmCode="";
		/**
		 * 是否查询任务页面
		 */
		$scope.isTaskQuery = "true";

		/**
		 * 详情页面，默认显示报告预览页面
		 */
		$scope.detailQuery=0;
		/**
		 * 基本信息页面参数
		 */
		$scope.essential={};
		/**
		 * 查看页面主表数据集
		 */
		$scope.detailData={};
		/**
		 * 实际水样测定数据集
		 */
		$scope.WaterMeasureData={};
		/**
		 * 质控样品测定数据集
		 */
		$scope.SampleMeasureData={};
		/**
		 * 技术说明数据集
		 */
		$scope.ScienceExplainData={};
		/**
		 * 实际水样测定table行
		 */
		$scope.WaterTableRowList=[];
		/**
		 * 质控样品测定table行
		 */
		$scope.SampleTableRowList=[];
		
		$scope.addTableShow=false;
		/**
		 * 时间窗口
		 */
		$scope.timeDialog;
		/**
		 * 查看按钮事件，用于切换页面
		 */
		$scope.onReturnTaskQuery = function(station){
			$scope.isTaskQuery = true;
			$state.go("DataComparison.TaskQuery");
		}
		/**
		* 报告详情切换事件
		*/
		$scope.onDetailClick=function(){
			toggleDetail();
		}
	
		/**
		 * 报告详情页面保存事件
		 */
		$scope.onSaveClick=function(){
			SaveDetail();
		}
		/**
		 * 报告类型选择事件
		 */
		$scope.onReportTypeClick=function(){
			$scope.TestingReportData=[];
			if($scope.reportType==1){
				$scope.detailQuery=0;
				/**
				 * 验收比对报告
				 */
				$scope.TestingReportData=[{"k":0,"v":"报告预览"},
				      		        	{"k":1,"v":"基本信息"},{"k":2,"v":"实际水样测定"},{"k":3,"v":"质控样品测定"},{"k":4,"v":"技术说明"}];
			}else if($scope.reportType==2){
				$scope.detailQuery=5;
				/**
				 * 有效性比对报告
				 */
				$scope.TestingReportData=[{"k":5,"v":"报告预览"},
				         		        	{"k":6,"v":"基本信息"},{"k":7,"v":"比对测试"}];
			}
		}
		/**
		 * 实际水样误差计算事件
		 */
		$scope.onCalculateErrorClick=function(row,index){
			
			calculateError(row,index);
		}
		$scope.itemData=null;
		$scope.itemIndex=null;
		$scope.timeType=null;
		$scope.newTime;
		/**
		 * 时间窗口
		 */
        $scope.ontimeWin=function(row,index,type){
        	$scope.itemData=row;
        	$scope.itemIndex=index;
        	$scope.timeType=type;
        	if($scope.timeType=='0'){
        		$scope.newTime=$scope.itemData.SAMPLEBEGINTIME;
        	}
        	if($scope.timeType=='1'){
        		$scope.newTime=$scope.itemData.SAMPLEENDTIME;
        	}
        	$scope.timeDialog.open();
		}
        /**
         * 时间窗口确定
         */
        $scope.onTimeCheck=function(){
        	if($scope.timeType=='0'){
        		$scope.itemData.SAMPLEBEGINTIME=$scope.newTime;
        	}
        	if($scope.timeType=='1'){
        		$scope.itemData.SAMPLEENDTIME=$scope.newTime;
        	}
        	$scope.timeDialog.close();
        	calculateError($scope.itemData,$scope.itemIndex);
        }
		/**
		 * 实际水样误差全部计算事件,浓度值变更
		 */
		$scope.onTotalCalculateErrorClick=function(){
			for(var i=0;i<$scope.SampleTableRowList.length;i++){
				$scope.onCalculateErrorClick($scope.SampleTableRowList[i],i);
			}
			
		}
		/**
		 * 获取在线监测数据
		 */
		$scope.onGetMonitorDataMinClick=function(row){
			if(row.LABORATORYVALUE!=''&&row.LABORATORYVALUE!=null){
			 $scope.queryObject.PARMNAME=$scope.param;
			 $scope.queryObject.STATIONCODE=$scope.detailData.MN;
			 $scope.queryObject.SAMPLEBEGINTIME=row.SAMPLEBEGINTIME;
			 $scope.queryObject.SAMPLEENDTIME=row.SAMPLEENDTIME;
			 $scope.queryObject.LABORATORYVALUE=row.LABORATORYVALUE;			
			 $dataComparisonService.getMonitorDataMin($scope.queryObject).then(function(data){
				 if(data.data.length>0){
					 row.ONLINEINSVALUE=data.data[0].monitorData.AVG+row.LABORATORYVALUE;
				 }});	
			}
		}
		/**
		 * 添加table行事件
		 */
		$scope.onAddTableRowClick=function(){
			addTableRow();
		}
		
		/**
		 * 删除table行事件
		 */
		$scope.onDetTableRowClick=function(index,GUID){
			deleteTableRow(index,GUID);
		}
		/**
		* 报告详情切换事件
		*/
		function toggleDetail(){
			$scope.queryObject={};
			$scope.queryObject.REPORTNUMBER=$scope.detailData.REPORTNUMBER;
			 $scope.queryObject.PARMNAME=$scope.param;
			 $scope.queryObject.STATIONCODE=$scope.detailData.MN;
			if($scope.detailQuery==0||$scope.detailQuery==5){//报告预览	
				$scope.addTableShow=false;
				reportPreview();
			 }else if($scope.detailQuery==1||$scope.detailQuery==6){//1：基本信息
				 $scope.addTableShow=false;
				 $dataComparisonService.getComparisonList($scope.queryObject).then(function(data){
					 $scope.essential=data.data[0];
				 });			
			 }else if($scope.detailQuery==2||$scope.detailQuery=='7'){//实际水样测定
				 WaterMeasure();
				 if($scope.param=='029'||$scope.param=='024'||$scope.param=='028'){
					 $scope.addTableShow=false;
				 }else{
					 if($scope.detailQuery=='7'){
						 $scope.addTableShow=false;
					 }else{
						 $scope.addTableShow=true;
					 }
					
				 }
					$dataComparisonService.getMoinitorParam( $scope.queryObject).then(function(data){				
						$scope.essential.S_DOMAINCAPTION=data.data[0].S_DOMAINCAPTION;	
					});		
			 }else if ($scope.detailQuery==3){//质控样品测定	
				 sampleMeasure();
				 if($scope.param=='029'||$scope.param=='024'||$scope.param=='028'){
					 $scope.addTableShow=false;
				 }else{
					 $scope.addTableShow=true;
				 }
				 $dataComparisonService.getMoinitorParam( $scope.queryObject).then(function(data){				
						$scope.essential.S_DOMAINCAPTION=data.data[0].S_DOMAINCAPTION;	
					});	
			 }else if($scope.detailQuery==4){//4：技术说明，
				 $scope.addTableShow=false;
				 $dataComparisonService.getComparisonList($scope.queryObject).then(function(data){
					 $scope.ScienceExplainData=data.data[0];
				 });				
			 }
		  };
		/**
		 * 查看页面保存按钮
		 */
		function SaveDetail(){
			$scope.NRESULT=0;
			if($scope.detailQuery=='1'){//1：基本信息/
				$scope.essential.PARMNAME=$scope.param;
				$scope.essential.REPORTNUMBER=$scope.detailData.REPORTNUMBER;
				$dataComparisonService.createComparisonModel($scope.essential).then(function(){
					$.messager.alert("提示","保存成功！",'info');
					createModel();
				});
			}else if($scope.detailQuery=='2'||$scope.detailQuery=='7'){//实际水样测定	
				$scope.waterdataType=true;
				if($scope.param!='029'&&$scope.param!='024'&&$scope.param!='028'){
					if($scope.detailData.REPORTTYPE=='0'){
						if($scope.WaterTableRowList.length<6){
							$.messager.alert("提示","验收比对报告至少需要6条实际水样测定才能保存！");
							return false;
						}else{
							for(var i=0;i<$scope.WaterTableRowList.length;i++){
								if($scope.WaterTableRowList[i].SAMPLENUMBER==null||$scope.WaterTableRowList[i].SAMPLENUMBER==''
									||$scope.WaterTableRowList[i].ONLINEINSVALUE==null||$scope.WaterTableRowList[i].ONLINEINSVALUE==''
									||$scope.WaterTableRowList[i].LABORATORYVALUE==null||$scope.WaterTableRowList[i].LABORATORYVALUE==''
									||$scope.WaterTableRowList[i].EVARESULTE==null||$scope.WaterTableRowList[i].EVARESULTE==''){
									$.messager.alert("提示","数据不完整(样品编号、测定值、结果判定不能为空！)",'info');
									return false;
								}
							}
						}
					}else{
						if($scope.WaterTableRowList.length<3){
							$.messager.alert("提示","有效性比对报告至少需要3条实际水样测定才能保存！");
							return false;
						}else{
							for(var i=0;i<$scope.WaterTableRowList.length;i++){
								if($scope.WaterTableRowList[i].SAMPLENUMBER==null||$scope.WaterTableRowList[i].SAMPLENUMBER==''
									||$scope.WaterTableRowList[i].ONLINEINSVALUE==null||$scope.WaterTableRowList[i].ONLINEINSVALUE==''
									||$scope.WaterTableRowList[i].LABORATORYVALUE==null||$scope.WaterTableRowList[i].LABORATORYVALUE==''
									||$scope.WaterTableRowList[i].EVARESULTE==null||$scope.WaterTableRowList[i].EVARESULTE==''){
									
									$.messager.alert("提示","数据不完整(样品编号、测定值、结果判定不能为空！)",'info');
									return false;
								}
							}
						}
					}
					waterMeasureSave();
				}else{
					if($scope.WaterTableRowList[9].LABORATORYVALUE==0||
							$scope.WaterTableRowList[19].LABORATORYVALUE==0||$scope.WaterTableRowList[29].LABORATORYVALUE==0){
						$.messager.alert("提示","平均值不能为0！");
						return false;
					}
					if($scope.waterdataType==false){
						return false;
					}
					waterMeasureSave();
					
				}
				
				$scope.queryObject.REPORTNUMBER=$scope.detailData.REPORTNUMBER;
				$scope.queryObject.PARMNAME=$scope.param;
				if($scope.param=='029'||$scope.param=='024'||$scope.param=='028'){
					$scope.NRESULT=$scope.NRESULT/9;
					if($scope.NRESULT<3){
						//不合格						
						$scope.queryObject.COMPARISONRESULT=1;
					}else{
						//合格
						$scope.queryObject.COMPARISONRESULT=0;
					}
				}else{
					if($scope.detailData.REPORTTYPE==0){//验收比对
						if($scope.NRESULT<5){
							//不合格						
							$scope.queryObject.COMPARISONRESULT=1;
						}else{
							//合格
							$scope.queryObject.COMPARISONRESULT=0;
						}
					}else if($scope.detailData.REPORTTYPE==1){
						if($scope.NRESULT<2){
							//不合格
							$scope.queryObject.COMPARISONRESULT=1;
						}else{
							//合格
							$scope.queryObject.COMPARISONRESULT=0;
						}
					}
				}
				
				$dataComparisonService.updatedatacomparisoninfo($scope.queryObject).then(function(){
					$.messager.alert("提示","保存成功！",'info');						
				});
			}else if ($scope.detailQuery=='3'){//质控样品测定	
				sampleMeasureSave();	
			}else if($scope.detailQuery=='4'){//4：技术说明，
				$scope.ScienceExplainData.PARMNAME=$scope.param;
				$scope.ScienceExplainData.REPORTNUMBER=$scope.detailData.REPORTNUMBER;
				$dataComparisonService.createComparisonModel($scope.ScienceExplainData).then(function(){
					$.messager.alert("提示","保存成功！",'info');
					createModel();
				});
			}else if($scope.detailQuery=='6'){//比对监测情况-基本信息
				$scope.monitorEquipment={};
				$scope.monitorEquipment.NAME=$scope.essential.NAME;
				$scope.monitorEquipment.MANUFACTURER=$scope.essential.MANUFACTURER;
				$scope.monitorEquipment.DEVICE_TYPENUM=$scope.essential.DEVICE_TYPENUM;
				$scope.monitorEquipment.DEVICE_NUM=$scope.essential.DEVICE_NUM;
				$dataComparisonService.updateMonitorEquipment($scope.monitorEquipment);
				$.messager.alert("提示","保存成功！",'info');
				createModel();
			}
		}
	
	
		/**
		 * 提交新建按钮点击事件
		 */
		$scope.onSubmitCreateClick = function(data){
			//新建时候处理，若parmCode为空，则选择全部参数
			if(parmCode =="")
			{
				parmCode=$scope.selectedModel.parmToChecksStr;
				$scope.dataComparisonCreate.PARMNAME = parmCode;
				data.PARMNAME = parmCode;
			}			
			$dataComparisonService.createModel(data).then(function(){
				var parm=data.PARMNAME.split(",");
				for(var i=0;i<parm.length-1;i++){
					$scope.Comparisondata={};
					$scope.Comparisondata.PARMNAME=parm[i];
					$scope.Comparisondata.REPORTNUMBER=data.REPORTNUMBER;
					$dataComparisonService.createComparisonModel($scope.Comparisondata).then(function(){});
				}
				$.messager.alert("保存成功","保存成功",'info');
				$scope.getDataComparisonSummaryList();
			});
		};
		
		/**
		 *误差计算方式
		 */
		function calculateError(row,index){		 
			if(row.SAMPLEBEGINTIME==""){
				 row.ABSOLUTEERROR=null;
				 row.ONLINEINSVALUE=null;
			     row.EVARESULTE="";
			     row.RELATIVEERROR=null;
				return false;
			}else{
				if(row.SAMPLEBEGINTIME!=null&&row.SAMPLEBEGINTIME!=undefined&&row.SAMPLEENDTIME!=null&&row.SAMPLEENDTIME!=undefined){
					if((new Date(row.SAMPLEBEGINTIME.replace(/-/g,"\/"))) > (new Date(row.SAMPLEENDTIME.replace(/-/g,"\/")))){
				    	$.messager.alert("提示","起始时间不能大于结束时间");
				      return false;
				    }
				}
			}						
			if($scope.detailQuery=='2'||$scope.detailQuery=='7'){
			if(row.LABORATORYVALUE!=''&&row.LABORATORYVALUE!=null){
				 $scope.queryObject.PARMNAME=$scope.param;
				 $scope.queryObject.STATIONCODE=$scope.detailData.MN;
				 $scope.queryObject.SAMPLEBEGINTIME=row.SAMPLEBEGINTIME;
				 $scope.queryObject.SAMPLEENDTIME=row.SAMPLEENDTIME;
				 if($scope.queryObject.SAMPLEENDTIME==""||$scope.queryObject.SAMPLEENDTIME==null||$scope.queryObject.SAMPLEENDTIME==undefined){
					 //COD、氨氮需要10~40分钟的分析时间，所以我们获取的值是开始时间之后的40分钟以内最接近实验值的数据
					 //当结束时间为空的时候，结束时间=开始时间+40分钟
					 if($scope.param=='011'||$scope.param=='060'){
						 var time=new Date(row.SAMPLEBEGINTIME);									
						 var hout=$scope.dateFormat(time.getHours());
						 var min=$scope.dateFormat(time.getMinutes()+40);
						 var month=$scope.dateFormat(time.getMonth()+1);
						 var day=$scope.dateFormat(time.getDate());
						 if(min>60){
							 hout=hout+1; 
							 min=min-60;
						 }else if(min==60){
							 hout=hout+1; 
							 min=00;
						 }
						 $scope.queryObject.SAMPLEENDTIME=time.getFullYear()+"-"+month+"-"+day+" "+hout+":"+min+":00";
					 }
				 }				
				 if(row.LABORATORYVALUE.toUpperCase()=="ND"){
					 if($scope.ScienceExplainData.LI_DETECTION_LIMIT!=""&&$scope.ScienceExplainData.LI_DETECTION_LIMIT!=undefined){
						 $scope.queryObject.LABORATORYVALUE=$scope.ScienceExplainData.LI_DETECTION_LIMIT/2;	 
					 }else{
						 $.messager.alert("提示","请填写检出限！",'info');
						return false; 
					 }		 
				 }else{
					 $scope.queryObject.LABORATORYVALUE=parseFloat(row.LABORATORYVALUE);	 
				 }	
				 $dataComparisonService.getMonitorDataMin($scope.queryObject).then(function(data){
					 if(data.data.length>0&&data.data[0].monitorData!=undefined){
						if(data.data[0].monitorData.AVG!=""||data.data[0].monitorData.AVG!=null){
						 row.ONLINEINSVALUE=parseFloat(data.data[0].monitorData.AVG).toFixed(2);
						//绝对误差
							var absoluteValue=Math.abs(row.ONLINEINSVALUE-$scope.queryObject.LABORATORYVALUE);
							//相对误差	
							if(row.LABORATORYVALUE.toUpperCase()=="ND"){
								var relativeValue=(Math.abs(row.ONLINEINSVALUE-$scope.queryObject.LABORATORYVALUE))/$scope.queryObject.LABORATORYVALUE*100;
							}else{
								var relativeValue=(Math.abs(row.ONLINEINSVALUE-$scope.queryObject.LABORATORYVALUE))/$scope.queryObject.LABORATORYVALUE*100;
							}							
							if(row.ONLINEINSVALUE!=""&&row.LABORATORYVALUE!=""){
								//pH
								if($scope.param=='001'||$scope.param=='phtem'){				
									if(absoluteValue>=-0.5&&absoluteValue<=0.5){
										row.EVARESULTE="0";
										row.ABSOLUTEERROR=absoluteValue.toFixed(2);
										row.RELATIVEERROR=null;
									}else{
										row.EVARESULTE="1";
										row.ABSOLUTEERROR=absoluteValue.toFixed(2);
										row.RELATIVEERROR=null;
									}
									row.LIMITINGVALUE="绝对误差不超过±0.5pH"
								//氨氮、总氮、总磷
								}else if($scope.param=='060'||$scope.param=='065'||$scope.param=='101'){
									if(relativeValue>=-15&&relativeValue<=15){
										row.EVARESULTE="0";
										row.ABSOLUTEERROR=null;
										row.RELATIVEERROR=relativeValue.toFixed(2);
									}else{
										row.EVARESULTE="1";
										row.ABSOLUTEERROR=null;
										row.RELATIVEERROR=relativeValue.toFixed(2);
									}
									row.LIMITINGVALUE="相对误差不超过±15%"
									//总铜
								}else if($scope.param=='029'){
									if(row.ONLINEINSVALUE<=0.2){
										if(absoluteValue>=-0.05&&absoluteValue<=0.05){
											row.EVARESULTE="0";
											row.RELATIVEERROR=null;
											row.ABSOLUTEERROR=absoluteValue.toFixed(2);
										}else{
											row.EVARESULTE="1";
											row.RELATIVEERROR=null;
											row.ABSOLUTEERROR=absoluteValue.toFixed(2);
										}
										row.LIMITINGVALUE="绝对误差不超过≤±0.05mg/L"
									}else if(row.ONLINEINSVALUE>0.2&&row.ONLINEINSVALUE<=5){					
										if(relativeValue>=-20&&relativeValue<=20){
											row.EVARESULTE="0";
											row.ABSOLUTEERROR=null;
											row.RELATIVEERROR=relativeValue.toFixed(2);
										}else{
											row.EVARESULTE="1";
											row.ABSOLUTEERROR=null;
											row.RELATIVEERROR=relativeValue.toFixed(2);
										}
										row.LIMITINGVALUE="相对误差不超过≤±20%"
									}
									//六价铬
								}else if($scope.param=='024'){
									if(row.ONLINEINSVALUE<=0.5){
										if(relativeValue<=15){
											row.EVARESULTE="0";
											row.RELATIVEERROR=relativeValue.toFixed(2);
											row.ABSOLUTEERROR=null
										}else{
											row.EVARESULTE="1";
											row.RELATIVEERROR=relativeValue.toFixed(2);
											row.ABSOLUTEERROR=null;
										}
										row.LIMITINGVALUE="相对误差≤15%"
									}else if(row.ONLINEINSVALUE>0.5){
										if(relativeValue<=10){
											row.EVARESULTE="0";
											row.RELATIVEERROR=relativeValue.toFixed(2);
											row.ABSOLUTEERROR=null;
										}else{
											row.EVARESULTE="1";
											row.RELATIVEERROR=relativeValue.toFixed(2);
											row.ABSOLUTEERROR=null;
										}
										row.LIMITINGVALUE="相对误差≤10%"
									}
									//化学需氧量，总有机碳
								}else if($scope.param=='011'||$scope.param=='015'){
									if(row.LABORATORYVALUE<30){
										if(absoluteValue>=-5&&absoluteValue<=5){
											row.EVARESULTE="0";
											row.RELATIVEERROR=null;
											row.ABSOLUTEERROR=absoluteValue.toFixed(2);
										}else{
											row.EVARESULTE="1";
											row.RELATIVEERROR=null;
											row.ABSOLUTEERROR=absoluteValue.toFixed(2);
										}
										row.LIMITINGVALUE="绝对误差不超过±5mg/L"
									}else if(row.LABORATORYVALUE>=30&&row.LABORATORYVALUE<=60){
										if(relativeValue>=-30&&relativeValue<=30){
											row.EVARESULTE="0";
											row.RELATIVEERROR=relativeValue.toFixed(2);
											row.ABSOLUTEERROR=null;
										}else{
											row.EVARESULTE="1";
											row.RELATIVEERROR=relativeValue.toFixed(2);
											row.ABSOLUTEERROR=null;
										}
										row.LIMITINGVALUE="相对误差不超过±30%"
									}else if(row.LABORATORYVALUE>=60&&row.LABORATORYVALUE<100){
										if(relativeValue>=-20&&relativeValue<=20){
											row.EVARESULTE="0";
											row.RELATIVEERROR=relativeValue.toFixed(2);
											row.ABSOLUTEERROR=null;
										}else{
											row.EVARESULTE="1";
											row.RELATIVEERROR=relativeValue.toFixed(2);
											row.ABSOLUTEERROR=null;
										}
										row.LIMITINGVALUE="相对误差不超过±20%"
									}else if(row.LABORATORYVALUE>=100){
										if(relativeValue>=-15&&relativeValue<=15){
											row.EVARESULTE="0";
											row.RELATIVEERROR=relativeValue.toFixed(2);
											row.ABSOLUTEERROR=null;
										}else{
											row.EVARESULTE="1";
											row.RELATIVEERROR=relativeValue.toFixed(2);
											row.ABSOLUTEERROR=null;
										}
										row.LIMITINGVALUE="相对误差不超过±15%"
									}
								}
								}else{
									row.EVARESULTE="";
									row.RELATIVEERROR="";
								}
					 }
					 }else{
						 row.ABSOLUTEERROR=null;
						 row.ONLINEINSVALUE=null;
					     row.EVARESULTE="";
					 }
					 
				 });					
				}else{
					row.RELATIVEERROR="";
					row.ABSOLUTEERROR=null;
					row.ONLINEINSVALUE=null;
					row.EVARESULTE="";
				}
			
			}else if($scope.detailQuery=='3'){
				if($scope.param!='060'&&$scope.param!='101'&&$scope.param!='011'){
					$.messager.alert("提示","质控样品只做总磷、氨氮、COD的测定",'info');
					return;
				}
				if($scope.STASAMPLECON=="0"){
					$.messager.alert("提示","浓度值不能为0！",'info');
					return false;
				}
				if(row.SAMPLEBEGINTIME!=""&&row.SAMPLEENDTIME!=""&&row.SAMPLEBEGINTIME!=undefined&&row.SAMPLEENDTIME!=undefined&&$scope.STASAMPLECON!=""&&!isNaN(row.ONLINEINSVALUE)&&$scope.STASAMPLECON!=undefined){
					 $scope.queryObject.PARMNAME=$scope.param;
					 $scope.queryObject.STATIONCODE=$scope.detailData.MN;
					 $scope.queryObject.SAMPLEBEGINTIME=row.SAMPLEBEGINTIME;
					 $scope.queryObject.SAMPLEENDTIME=row.SAMPLEENDTIME;
					 $scope.queryObject.LABORATORYVALUE=$scope.STASAMPLECON;			
					 $dataComparisonService.getMonitorDataMin($scope.queryObject).then(function(data){
						 if(data.data.length>0&&data.data[0].monitorData!=undefined){
							 row.LABORATORYVALUE=parseFloat(data.data[0].monitorData.AVG);
							    //绝对误差
								row.ABSOLUTEERROR=(parseFloat(row.LABORATORYVALUE)-parseFloat($scope.STASAMPLECON)).toFixed(2);
								//相对误差			
								row.RELATIVEERROR=((parseFloat(row.LABORATORYVALUE)-parseFloat($scope.STASAMPLECON))/parseFloat($scope.STASAMPLECON)).toFixed(2)*100;
								if($scope.param=='060'||$scope.param=='101'){
									if(row.ABSOLUTEERROR<=0.1){
										row.EVARESULTE="0";
									}else{
										row.EVARESULTE="1";
									}
								}
								if($scope.param=='011'){
									if(row.ABSOLUTEERROR<=5){
										row.EVARESULTE="0";
									}else{
										row.EVARESULTE="1";
									}
								}
						 }else{
							row.EVARESULTE="";
							row.RELATIVEERROR="";
							row.ABSOLUTEERROR=null;
							row.LABORATORYVALUE=null;
						}						 
					 });
				}else{
					row.EVARESULTE="";
					row.ABSOLUTEERROR=null;
					row.RELATIVEERROR="";
					row.LABORATORYVALUE=null;
				}

			}
			
		}
		/**
		 * 重金属实际水样误差计算方式
		 */
		$scope.calculateErrorWater=function(row,index){			
			 if(index>=0&&index<10){
				 row.SAMPLEBEGINTIME=$scope.WaterTableRowList[0].SAMPLEBEGINTIME;
				 row.SAMPLEENDTIME=$scope.WaterTableRowList[0].SAMPLEENDTIME;
			 }else if(index>=10&&index<20){
				 row.SAMPLEBEGINTIME=$scope.WaterTableRowList[10].SAMPLEBEGINTIME;
				 row.SAMPLEENDTIME=$scope.WaterTableRowList[10].SAMPLEENDTIME;
			 }else{
				 row.SAMPLEBEGINTIME=$scope.WaterTableRowList[20].SAMPLEBEGINTIME;
				 row.SAMPLEENDTIME=$scope.WaterTableRowList[20].SAMPLEENDTIME;
			 }
			 if(row.SAMPLEBEGINTIME!=null&&row.SAMPLEBEGINTIME!=undefined&&row.SAMPLEENDTIME!=null&&row.SAMPLEENDTIME!=undefined){
				 if((new Date(row.SAMPLEBEGINTIME.replace(/-/g,"\/"))) > (new Date(row.SAMPLEENDTIME.replace(/-/g,"\/")))){
				    	$.messager.alert("提示","起始时间不能大于结束时间");
				      return false;
				    } 
			 }			 	
			if(row.SAMPLEBEGINTIME!=''&&row.SAMPLEENDTIME!=''){
					 $scope.queryObject.PARMNAME=$scope.param;
					 $scope.queryObject.STATIONCODE=$scope.detailData.MN;
					 $scope.queryObject.SAMPLEBEGINTIME=row.SAMPLEBEGINTIME;
					 $scope.queryObject.SAMPLEENDTIME=row.SAMPLEENDTIME;
					 
					 var index0=0;
					 var index3=0;
					 var index6=0;	
					 if(index>=0&&index<10){
						 index0=parseFloat($scope.WaterTableRowList[0].LABORATORYVALUE);
						 index3=parseFloat($scope.WaterTableRowList[3].LABORATORYVALUE);
						 index6=parseFloat($scope.WaterTableRowList[6].LABORATORYVALUE);
						 index=0;							
					 }else if(index>=10&&index<20){
						 index0=parseFloat($scope.WaterTableRowList[10].LABORATORYVALUE);
						 index3=parseFloat($scope.WaterTableRowList[13].LABORATORYVALUE);
						 index6=parseFloat($scope.WaterTableRowList[16].LABORATORYVALUE);
						 index=10;
					 }else{
						 index0=parseFloat($scope.WaterTableRowList[20].LABORATORYVALUE);
						 index3=parseFloat($scope.WaterTableRowList[23].LABORATORYVALUE);
						 index6=parseFloat($scope.WaterTableRowList[26].LABORATORYVALUE);		
						 index=20;
					 }
					 if(isNaN(index0)||isNaN(index3)||isNaN(index6)){
						 $scope.WaterTableRowList[index].EVARESULTE=null;
						 $scope.WaterTableRowList[index].RELATIVEERROR=null;
						 $scope.WaterTableRowList[index+9].LABORATORYVALUE=null;
						 for(var i=0;i<9;i++){
							 $scope.WaterTableRowList[index+i].ONLINEINSVALUE=null;					
						 }
						 return false;
					 }
					 var avg=(index0+index3+index6)/3;
					 if(avg==0){
						 $.messager.alert("提示","平均值不能为0！");
						 $scope.WaterTableRowList[index+9].LABORATORYVALUE=avg.toFixed(2);
						 return false;
					 }
					 $scope.queryObject.LABORATORYVALUE=avg;
					 $dataComparisonService.getMonitorDataMin($scope.queryObject).then(function(data){
						 if(data.data[0].monitorData.length>0){
							 var ONLINEINSVALUESUM=0;							 															
							 var waterSum=
								 ((Math.abs((data.data[0].monitorData[0].AVG+data.data[0].monitorData[1].AVG+data.data[0].monitorData[2].AVG)-avg*3))
										 +(Math.abs((data.data[0].monitorData[3].AVG+data.data[0].monitorData[4].AVG+data.data[0].monitorData[5].AVG)-avg*3))
										 +(Math.abs((data.data[0].monitorData[6].AVG+data.data[0].monitorData[7].AVG+data.data[0].monitorData[8].AVG)
										-avg*3)))/(avg*9);
							
							 for(var i=0;i<9;i++){
								 $scope.WaterTableRowList[index+i].ONLINEINSVALUE=0;
								 $scope.WaterTableRowList[index+i].ONLINEINSVALUE=data.data[0].monitorData[i].AVG;				
							 }
							 //总铜
							 if($scope.param=='029'){
									if(avg>=0.1&&avg<=0.5){
										if(waterSum<=0.2){
											$scope.WaterTableRowList[index].EVARESULTE="0";										
											$scope.WaterTableRowList[index].RELATIVEERROR=(waterSum*100).toFixed(2);
										}else{
											$scope.WaterTableRowList[index].EVARESULTE="1";
											$scope.WaterTableRowList[index].RELATIVEERROR=(waterSum*100).toFixed(2);
										}
										$scope.WaterTableRowList[index].LIMITINGVALUE="相对误差不超过≤20%"
									}else if(avg>0.5){					
										if(waterSum<=0.15){
											$scope.WaterTableRowList[index].EVARESULTE="0";
											$scope.WaterTableRowList[index].RELATIVEERROR=(waterSum*100).toFixed(2);
										}else{
											$scope.WaterTableRowList[index].EVARESULTE="1";
											$scope.WaterTableRowList[index].RELATIVEERROR=(waterSum*100).toFixed(2);
										}
										$scope.WaterTableRowList[index].LIMITINGVALUE="相对误差不超过≤15%"
									}else{
										$scope.WaterTableRowList[index].EVARESULTE="1";
										$scope.WaterTableRowList[index].RELATIVEERROR=(waterSum*100).toFixed(2);
										$scope.WaterTableRowList[index].LIMITINGVALUE="相对误差不超过≤15%"
									}
									//六价铬
								}else if($scope.param=='024'){
									if(avg<=0.5){
										if(waterSum<=0.15){
											$scope.WaterTableRowList[index].EVARESULTE="0";
											$scope.WaterTableRowList[index].RELATIVEERROR=(waterSum*100).toFixed(2);
										}else{
											$scope.WaterTableRowList[index].EVARESULTE="1";
											$scope.WaterTableRowList[index].RELATIVEERROR=(waterSum*100).toFixed(2);
										}
										$scope.WaterTableRowList[index].LIMITINGVALUE="相对误差≤15%"
									}else if(avg>0.5){
										if(waterSum<=0.1){
											$scope.WaterTableRowList[index].EVARESULTE="0";
											$scope.WaterTableRowList[index].RELATIVEERROR=(waterSum*100).toFixed(2);
										}else{
											$scope.WaterTableRowList[index].EVARESULTE="1";
											$scope.WaterTableRowList[index].RELATIVEERROR=(waterSum*100).toFixed(2);
										}
										$scope.WaterTableRowList[index].LIMITINGVALUE="相对误差≤10%"
									}else{
										$scope.WaterTableRowList[index].EVARESULTE="";
									}
									//总镍
								}else if($scope.param=='028'){
									if(avg<=0.5){
										if(waterSum<=0.25){
											$scope.WaterTableRowList[index].EVARESULTE="0";
											$scope.WaterTableRowList[index].RELATIVEERROR=(waterSum*100).toFixed(2);
										}else{
											$scope.WaterTableRowList[index].EVARESULTE="1";
											$scope.WaterTableRowList[index].RELATIVEERROR=(waterSum*100).toFixed(2);
										}
										$scope.WaterTableRowList[index].LIMITINGVALUE="相对误差≤25%"
									}else if(avg>0.5){
										if(waterSum<=0.15){
											$scope.WaterTableRowList[index].EVARESULTE="0";
											$scope.WaterTableRowList[index].RELATIVEERROR=(waterSum*100).toFixed(2);
										}else{
											$scope.WaterTableRowList[index].EVARESULTE="1";
											$scope.WaterTableRowList[index].RELATIVEERROR=(waterSum*100).toFixed(2);
										}
										$scope.WaterTableRowList[index].LIMITINGVALUE="相对误差≤15%"
									}
								}
							 $scope.WaterTableRowList[index+9].LABORATORYVALUE=avg.toFixed(2);
						 }
					 });	
			}		
		}
		/**
		 * 加任务行
		 */
		function addTableRow(){			
			if($scope.detailQuery=='2'||$scope.detailQuery=='7'){//实际水样测定
				if($scope.detailData.REPORTTYPE=='0'){
					if($scope.WaterTableRowList.length==6){
						$.messager.alert("提示","验收比对报告只能填写6条实际水样测定记录！");
						return false;
					}
				}else{
					if($scope.WaterTableRowList.length==3){
						$.messager.alert("提示","有效性比对报告只能填写3条实际水样测定记录！");
						return false;
					}
				}
				var obj={SAMPLENUMBER:"",GUID:"",ABSOLUTEERROR:"",SAMPLEENDTIME:"",REPORTNUMBER:$scope.detailData.REPORTNUMBER,SAMPLEBEGINTIME:"",ONLINEINSVALUE:"",LABORATORYVALUE:null,RELATIVEERROR:null,EVARESULTE:"",REMARK:"",DETERTYPE:"0"};
				$scope.WaterTableRowList.push(obj);
			}else if ($scope.detailQuery=='3'){//质控样品测定				
				var obj={SAMPLENUMBER:"",GUID:"",REPORTNUMBER:$scope.detailData.REPORTNUMBER,SAMPLEBEGINTIME:"",SAMPLEENDTIME:"",ONLINEINSVALUE:"",LABORATORYVALUE:null,RELATIVEERROR:null,EVARESULTE:"",REMARK:"",DETERTYPE:"1"};
				$scope.SampleTableRowList.push(obj);
			}			
		}
		/**
		 * 删除任务
		 */
		function deleteTableRow(index,GUID){
			$.messager.confirm("确认","确定要删除这条记录吗？",function(r){
				if(r){	
					if($scope.detailQuery=='2'||$scope.detailQuery=='7'){//实际水样测定
						$scope.WaterTableRowList.splice(index, 1);
					}else if ($scope.detailQuery=='3'){//质控样品测定				
						$scope.SampleTableRowList.splice(index, 1);
					}
					if(GUID!=""||GUID!=undefined){
						$dataComparisonService.deleteComparisonDetailModel(GUID).then(function(){
							$.messager.alert("删除成功","删除成功",'info');								
						});
						
					}
				}
			});
		}
		$scope.Limitingvalue=function(row){			
					//pH
					if($scope.param=='001'||$scope.param=='phtem'){										
						row.LIMITINGVALUE="绝对误差不超过±0.5pH"
					//氨氮、总氮、总磷
					}else if($scope.param=='060'||$scope.param=='065'||$scope.param=='101'){						
						row.LIMITINGVALUE="相对误差不超过±15%"
						//总铜
					}else if($scope.param=='029'){
						if(row.LABORATORYVALUE>=0.1&&row.LABORATORYVALUE<=0.5){						
							row.LIMITINGVALUE="相对对误差不超过≤20%"
						}else if(row.LABORATORYVALUE>0.5){												
							row.LIMITINGVALUE="相对误差不超过≤±15%"
						}
						//六价铬
					}else if($scope.param=='024'){
						if(row.LABORATORYVALUE<=0.5){
							row.LIMITINGVALUE="绝对误差≤15%"
						}else if(row.LABORATORYVALUE>0.5){
							row.LIMITINGVALUE="相对误差≤10%"
						}
						//化学需氧量，总有机碳
					}else if($scope.param=='011'||$scope.param=='015'){
						if(row.LABORATORYVALUE<30){
							row.LIMITINGVALUE="绝对误差不超过±5mg/L"
						}else if(row.LABORATORYVALUE>=30&&row.LABORATORYVALUE<=60){
							row.LIMITINGVALUE="相对误差不超过±30%"
						}else if(row.LABORATORYVALUE>=60&&row.LABORATORYVALUE<100){
							row.LIMITINGVALUE="相对误差不超过±20%"
						}else if(row.LABORATORYVALUE>=100){							
							row.LIMITINGVALUE="相对误差不超过±15%"
						}
					}	
			return row.LIMITINGVALUE;
		}	
		/**
		 * 实际水样测定数据
		 */
		function WaterMeasure(){
			 $scope.queryObject.DETERTYPE='0';					
			 $dataComparisonService.getComparisonDetailList($scope.queryObject).then(function(data){
				 if(data.data.length>0){
					 $scope.WaterTableRowList=[];
					 for(var i=0;i<data.data.length;i++){
						 if($scope.param!='029'&&$scope.param!='024'&&$scope.param!='028'){
							 var WaterData=data.data[i];
							 var obj={GUID:WaterData.GUID,SAMPLENUMBER:WaterData.SAMPLENUMBER,REPORTNUMBER:$scope.detailData.REPORTNUMBER,
									 SAMPLEBEGINTIME:WaterData.SAMPLEBEGINTIME,SAMPLEENDTIME:WaterData.SAMPLEENDTIME,ONLINEINSVALUE:WaterData.ONLINEINSVALUE,
									 LABORATORYVALUE:WaterData.LABORATORYVALUE,RELATIVEERROR:WaterData.RELATIVEERROR,ABSOLUTEERROR:WaterData.ABSOLUTEERROR,
									 EVARESULTE:WaterData.EVARESULTE,REMARK:WaterData.REMARK,DETERTYPE:WaterData.DETERTYPE}; 
							 $scope.WaterTableRowList.push(obj);
						 }else{
								var obj={};
								var WaterData=data.data[i];
								if(i>0&&i%9==0){
									var avg=0;
									if(i==9){
										avg=(parseFloat(data.data[0].LABORATORYVALUE)+parseFloat(data.data[3].LABORATORYVALUE)+parseFloat(data.data[6].LABORATORYVALUE))/3;
									}else if(i==18){
										avg=(parseFloat(data.data[9].LABORATORYVALUE)+parseFloat(data.data[12].LABORATORYVALUE)+parseFloat(data.data[15].LABORATORYVALUE))/3;
									}
									var obj={SAMPLENUMBER:"平均值",GUID:"",ABSOLUTEERROR:"",SAMPLEENDTIME:"",REPORTNUMBER:$scope.detailData.REPORTNUMBER,SAMPLEBEGINTIME:"",ONLINEINSVALUE:"",LABORATORYVALUE:avg.toFixed(2),RELATIVEERROR:null,EVARESULTE:"",REMARK:"",DETERTYPE:"0"};
									$scope.WaterTableRowList.push(obj);
									if(i>=18){
										var count=parseInt((i+2)/10);
									}else{
										var count=parseInt((i+1)/10);
									}
									var obj={count:count,GUID:WaterData.GUID,SAMPLENUMBER:WaterData.SAMPLENUMBER,REPORTNUMBER:$scope.detailData.REPORTNUMBER,
												 SAMPLEBEGINTIME:WaterData.SAMPLEBEGINTIME,SAMPLEENDTIME:WaterData.SAMPLEENDTIME,ONLINEINSVALUE:WaterData.ONLINEINSVALUE,
												 LABORATORYVALUE:WaterData.LABORATORYVALUE,RELATIVEERROR:WaterData.RELATIVEERROR,ABSOLUTEERROR:WaterData.ABSOLUTEERROR,
												 EVARESULTE:WaterData.EVARESULTE,REMARK:WaterData.REMARK,DETERTYPE:WaterData.DETERTYPE}; 
									$scope.WaterTableRowList.push(obj);
								}else{
									if(i<9){
										var obj={GUID:WaterData.GUID,SAMPLENUMBER:WaterData.SAMPLENUMBER,REPORTNUMBER:$scope.detailData.REPORTNUMBER,
													 SAMPLEBEGINTIME:WaterData.SAMPLEBEGINTIME,SAMPLEENDTIME:WaterData.SAMPLEENDTIME,ONLINEINSVALUE:WaterData.ONLINEINSVALUE,
													 LABORATORYVALUE:WaterData.LABORATORYVALUE,RELATIVEERROR:WaterData.RELATIVEERROR,ABSOLUTEERROR:WaterData.ABSOLUTEERROR,
													 EVARESULTE:WaterData.EVARESULTE,REMARK:WaterData.REMARK,DETERTYPE:WaterData.DETERTYPE}; 
										$scope.WaterTableRowList.push(obj);
									}else{
										if(i>=18){
											var count=parseInt((i+2)/10);
										}else{
											var count=parseInt((i+1)/10);
										}
										var obj={count:count,GUID:WaterData.GUID,SAMPLENUMBER:WaterData.SAMPLENUMBER,REPORTNUMBER:$scope.detailData.REPORTNUMBER,
													SAMPLEBEGINTIME:WaterData.SAMPLEBEGINTIME,SAMPLEENDTIME:WaterData.SAMPLEENDTIME,ONLINEINSVALUE:WaterData.ONLINEINSVALUE,
													LABORATORYVALUE:WaterData.LABORATORYVALUE,RELATIVEERROR:WaterData.RELATIVEERROR,ABSOLUTEERROR:WaterData.ABSOLUTEERROR,
													EVARESULTE:WaterData.EVARESULTE,REMARK:WaterData.REMARK,DETERTYPE:WaterData.DETERTYPE}; 
										 $scope.WaterTableRowList.push(obj);
										 if(i==data.data.length-1){
											 var avg=(parseFloat(data.data[18].LABORATORYVALUE)+parseFloat(data.data[21].LABORATORYVALUE)+parseFloat(data.data[24].LABORATORYVALUE))/3;
											 var obj={SAMPLENUMBER:"平均值",GUID:"",ABSOLUTEERROR:"",SAMPLEENDTIME:"",REPORTNUMBER:$scope.detailData.REPORTNUMBER,SAMPLEBEGINTIME:"",ONLINEINSVALUE:"",LABORATORYVALUE:avg.toFixed(2),RELATIVEERROR:null,EVARESULTE:"",REMARK:"",DETERTYPE:"0"};
												$scope.WaterTableRowList.push(obj); 
										 }
									}
										
								}								
							}								
						 }
				 }else{
					 $scope.WaterTableRowList=[];
					 if($scope.param!='029'&&$scope.param!='024'&&$scope.param!='028'){
						 var obj={SAMPLENUMBER:"",GUID:"",ABSOLUTEERROR:"",SAMPLEENDTIME:"",REPORTNUMBER:$scope.detailData.REPORTNUMBER,SAMPLEBEGINTIME:"",ONLINEINSVALUE:"",LABORATORYVALUE:null,RELATIVEERROR:null,EVARESULTE:"",REMARK:"",DETERTYPE:"0"};
						 if($scope.WaterTableRowList.length==0){
							 $scope.WaterTableRowList.push(obj);
						 }	
					 }else{
						for(var i=0;i<=29;i++){
							var obj={};
							if(i==9||i==19||i==29){
								var obj={SAMPLENUMBER:"平均值",GUID:"",ABSOLUTEERROR:"",SAMPLEENDTIME:"",REPORTNUMBER:$scope.detailData.REPORTNUMBER,SAMPLEBEGINTIME:"",ONLINEINSVALUE:"",LABORATORYVALUE:null,RELATIVEERROR:null,EVARESULTE:"",REMARK:"",DETERTYPE:"0"};
								$scope.WaterTableRowList.push(obj);
							}else{
								if(i<9){
									var obj={SAMPLENUMBER:"",GUID:"",ABSOLUTEERROR:"",SAMPLEENDTIME:"",REPORTNUMBER:$scope.detailData.REPORTNUMBER,SAMPLEBEGINTIME:"",ONLINEINSVALUE:"",LABORATORYVALUE:null,RELATIVEERROR:null,EVARESULTE:"",REMARK:"",DETERTYPE:"0"};
									$scope.WaterTableRowList.push(obj);
								}else{
									var count=parseInt(i/10);
									var obj={count:count,SAMPLENUMBER:"",GUID:"",ABSOLUTEERROR:"",SAMPLEENDTIME:"",REPORTNUMBER:$scope.detailData.REPORTNUMBER,SAMPLEBEGINTIME:"",ONLINEINSVALUE:"",LABORATORYVALUE:null,RELATIVEERROR:null,EVARESULTE:"",REMARK:"",DETERTYPE:"0"};
									$scope.WaterTableRowList.push(obj);
								}
								
							}								
						}		
					 }
				 }
			});				 			  		
		}
		/**
		 * 保存实际水样数据
		 */
		function waterMeasureSave(){
			for(var i=0;i<$scope.WaterTableRowList.length;i++){			
					$scope.WaterTableRowList[i].PARMNAME=$scope.param;					
					if($scope.param=='029'||$scope.param=='024'||$scope.param=='028'){
						$scope.waterData={};
						if($scope.WaterTableRowList[i].SAMPLENUMBER!='平均值'){
							if(i==0){
								$scope.waterData=$scope.WaterTableRowList[i];
							}else if(i>0&&i<10){
								if(i<=2){
									$scope.waterData=$scope.WaterTableRowList[i];
									$scope.waterData.SAMPLENUMBER=$scope.WaterTableRowList[0].SAMPLENUMBER;
									$scope.waterData.LABORATORYVALUE=$scope.WaterTableRowList[0].LABORATORYVALUE;
									$scope.waterData.EVARESULTE=$scope.WaterTableRowList[0].EVARESULTE;
								}else if(i>2&&i<=5){
									$scope.waterData=$scope.WaterTableRowList[i];
									$scope.waterData.SAMPLENUMBER=$scope.WaterTableRowList[3].SAMPLENUMBER;
									$scope.waterData.LABORATORYVALUE=$scope.WaterTableRowList[3].LABORATORYVALUE;
									$scope.waterData.EVARESULTE=$scope.WaterTableRowList[3].EVARESULTE;
								}else{
									$scope.waterData=$scope.WaterTableRowList[i];
									$scope.waterData.SAMPLENUMBER=$scope.WaterTableRowList[6].SAMPLENUMBER;
									$scope.waterData.LABORATORYVALUE=$scope.WaterTableRowList[6].LABORATORYVALUE;	
									$scope.waterData.EVARESULTE=$scope.WaterTableRowList[6].EVARESULTE;
								}
								$scope.waterData.SAMPLEBEGINTIME=$scope.WaterTableRowList[0].SAMPLEBEGINTIME;
								$scope.waterData.SAMPLEENDTIME=$scope.WaterTableRowList[0].SAMPLEENDTIME;
								$scope.waterData.REMARK=$scope.WaterTableRowList[0].REMARK;
								$scope.waterData.RELATIVEERROR=$scope.WaterTableRowList[0].RELATIVEERROR;
								$scope.waterData.EVARESULTE=$scope.WaterTableRowList[0].EVARESULTE;
							}else{
								$scope.waterData=$scope.WaterTableRowList[i];							
								if(i<20){
									if(i%3==0){
										$scope.waterData.SAMPLENUMBER=$scope.WaterTableRowList[i-i%3-2].SAMPLENUMBER;
										$scope.waterData.LABORATORYVALUE=$scope.WaterTableRowList[i-i%3-2].LABORATORYVALUE;		
										$scope.waterData.EVARESULTE=$scope.WaterTableRowList[i-i%3-2].EVARESULTE;
									}else{
										$scope.waterData.SAMPLENUMBER=$scope.WaterTableRowList[i-i%3+1].SAMPLENUMBER;
										$scope.waterData.LABORATORYVALUE=$scope.WaterTableRowList[i-i%3+1].LABORATORYVALUE;		
										$scope.waterData.EVARESULTE=$scope.WaterTableRowList[i-i%3+1].EVARESULTE;
									}
									$scope.waterData.SAMPLEBEGINTIME=$scope.WaterTableRowList[10].SAMPLEBEGINTIME;
									$scope.waterData.SAMPLEENDTIME=$scope.WaterTableRowList[10].SAMPLEENDTIME;
									$scope.waterData.REMARK=$scope.WaterTableRowList[10].REMARK;
									$scope.waterData.RELATIVEERROR=$scope.WaterTableRowList[10].RELATIVEERROR;
									$scope.waterData.EVARESULTE=$scope.WaterTableRowList[10].EVARESULTE;
								}else{
									if(i%3==0){
										$scope.waterData.SAMPLENUMBER=$scope.WaterTableRowList[i-i%3-1].SAMPLENUMBER;
										$scope.waterData.LABORATORYVALUE=$scope.WaterTableRowList[i-i%3-1].LABORATORYVALUE;	
										$scope.waterData.EVARESULTE=$scope.WaterTableRowList[i-i%3-1].EVARESULTE;
									}else{
										$scope.waterData.SAMPLENUMBER=$scope.WaterTableRowList[i-(i+1)%3].SAMPLENUMBER;
										$scope.waterData.LABORATORYVALUE=$scope.WaterTableRowList[i-(i+1)%3].LABORATORYVALUE;	
										$scope.waterData.EVARESULTE=$scope.WaterTableRowList[i-(i+1)%3].EVARESULTE;
									}
									$scope.waterData.SAMPLEBEGINTIME=$scope.WaterTableRowList[20].SAMPLEBEGINTIME;
									$scope.waterData.SAMPLEENDTIME=$scope.WaterTableRowList[20].SAMPLEENDTIME;
									$scope.waterData.REMARK=$scope.WaterTableRowList[20].REMARK;
									$scope.waterData.RELATIVEERROR=$scope.WaterTableRowList[20].RELATIVEERROR;
									$scope.waterData.EVARESULTE=$scope.WaterTableRowList[20].EVARESULTE;
								}
							}
						}	
						if($scope.waterData.SAMPLENUMBER!=""&&$scope.waterData.SAMPLENUMBER!=undefined){
							if($scope.waterData.SAMPLEBEGINTIME!=null&&$scope.waterData.SAMPLEBEGINTIME!=undefined&&$scope.waterData.SAMPLEENDTIME!=null&&$scope.waterData.SAMPLEENDTIME!=undefined){
								if((new Date($scope.waterData.SAMPLEBEGINTIME.replace(/-/g,"\/"))) > (new Date($scope.waterData.SAMPLEENDTIME.replace(/-/g,"\/")))){
							    	$.messager.alert("提示","起始时间不能大于结束时间");
							      return false;
							    }
							}	
							if($scope.waterData.SAMPLENUMBER==null||$scope.waterData.SAMPLENUMBER==''
								||$scope.waterData.ONLINEINSVALUE==null||$scope.waterData.ONLINEINSVALUE==''
								||$scope.waterData.LABORATORYVALUE==null||$scope.waterData.LABORATORYVALUE==''
								||$scope.waterData.EVARESULTE==null||$scope.waterData.EVARESULTE==''){
								$scope.waterdataType=false;
								$.messager.alert("提示","数据不完整(样品编号、测定值、结果判定不能为空！)",'info');
								return false;
							}if($scope.waterData.LABORATORYVALUE=="0"){
								$scope.waterdataType=false;
								return false; 
							}else{
								if($scope.WaterTableRowList[i].EVARESULTE==0&&$scope.WaterTableRowList[i].SAMPLENUMBER!='平均值'){
									$scope.NRESULT++;
								}
								$scope.waterData.SAMPLEBEGINTIME= $scope.waterData.SAMPLEBEGINTIME;
								$scope.waterData.SAMPLEENDTIME =$scope.waterData.SAMPLEENDTIME; 
								if($scope.waterData.ABSOLUTEERROR==null){
									$scope.waterData.ABSOLUTEERROR="";
								}
								if($scope.waterData.RELATIVEERROR==null){
									$scope.waterData.RELATIVEERROR="";
								}
								$dataComparisonService.createComparisonDetailModel($scope.waterData).then(function(){
									
								});
								$scope.waterdataType=true;
							}
								
						
						}				
					}else{
						if($scope.WaterTableRowList[i].SAMPLEBEGINTIME!=null&&$scope.WaterTableRowList[i].SAMPLEBEGINTIME!=undefined&&$scope.WaterTableRowList[i].SAMPLEENDTIME!=null&&$scope.WaterTableRowList[i].SAMPLEENDTIME!=undefined){
							 if((new Date($scope.WaterTableRowList[i].SAMPLEBEGINTIME.replace(/-/g,"\/"))) > (new Date($scope.WaterTableRowList[i].SAMPLEENDTIME.replace(/-/g,"\/")))){
							    	$.messager.alert("提示","起始时间不能大于结束时间");
							      return false;
							    }
						}				
						if(isNaN($scope.WaterTableRowList[i].ONLINEINSVALUE)){
							$scope.WaterTableRowList[i].ONLINEINSVALUE=0;	
						}
						if($scope.WaterTableRowList[i].EVARESULTE==0&&$scope.WaterTableRowList[i].SAMPLENUMBER!='平均值'){
							$scope.NRESULT++;
						}
						if($scope.WaterTableRowList[i].ABSOLUTEERROR==null){
							$scope.WaterTableRowList[i].ABSOLUTEERROR="";
						}
						if($scope.WaterTableRowList[i].RELATIVEERROR==null){
							$scope.WaterTableRowList[i].RELATIVEERROR="";
						}
						if($scope.WaterTableRowList[i].SAMPLEENDTIME==""){
							$scope.WaterTableRowList[i].SAMPLEENDTIME="null_sql_value";
						}
						$dataComparisonService.createComparisonDetailModel($scope.WaterTableRowList[i]).then(function(){
							
						});
					}				
			}
			if($scope.reportType==0){
				if($scope.WaterTableRowList.length>=6){									
					$scope.DATACOMPARISONSUMMARY={};
					$scope.DATACOMPARISONSUMMARY.ISINTACT=1;//报告完成
					$scope.DATACOMPARISONSUMMARY.GUID=$scope.detailData.GUID;
					$dataComparisonService.updateComparisonSummaryModel($scope.DATACOMPARISONSUMMARY).then(function(){
						createModel();
					});
				}
			}else if($scope.reportType==1){
				if($scope.WaterTableRowList.length>=3){
					$scope.DATACOMPARISONSUMMARY={};
					$scope.DATACOMPARISONSUMMARY.ISINTACT=1;//报告完成
					$scope.DATACOMPARISONSUMMARY.GUID=$scope.detailData.GUID;
					$dataComparisonService.updateComparisonSummaryModel($scope.DATACOMPARISONSUMMARY).then(function(){
						createModel();
					});
				}
			}
		
		}
		/**
		 * 质控样品
		 */
		function sampleMeasure(){
			$scope.queryObject.DETERTYPE='1';
			 if($scope.param!='060'&&$scope.param!='101'&&$scope.param!='011'){
				 $scope.SampleTableRowList={};
					$.messager.alert("提示","质控样品只做总磷、氨氮、COD的测定",'info');
					return;
				}
			 $dataComparisonService.getComparisonDetailList($scope.queryObject).then(function(data){
				 if(data.data.length>0){
					 $scope.SampleTableRowList=[];
					 $scope.STASAMPLECON=data.data[0].STASAMPLECON;
					 $scope.STASAMPLEBATCHNUMBER=data.data[0].STASAMPLEBATCHNUMBER;
					 for(var i=0;i<data.data.length;i++){
						 var SampleData=data.data[i];
						 var obj={GUID:SampleData.GUID,SAMPLENUMBER:SampleData.SAMPLENUMBER,REPORTNUMBER:$scope.detailData.REPORTNUMBER,
								 SAMPLEBEGINTIME:SampleData.SAMPLEBEGINTIME,ONLINEINSVALUE:SampleData.ONLINEINSVALUE,
								 LABORATORYVALUE:SampleData.LABORATORYVALUE,RELATIVEERROR:SampleData.RELATIVEERROR,ABSOLUTEERROR:SampleData.ABSOLUTEERROR,
								 SAMPLEENDTIME:SampleData.SAMPLEENDTIME,EVARESULTE:SampleData.EVARESULTE,REMARK:SampleData.REMARK,DETERTYPE:SampleData.DETERTYPE}; 
						 $scope.SampleTableRowList.push(obj);
					 }
				 }else{
					 $scope.SampleTableRowList=[];
					 var obj={SAMPLENUMBER:"",GUID:"",REPORTNUMBER:$scope.detailData.REPORTNUMBER,SAMPLEENDTIME:"",SAMPLEBEGINTIME:"",ABSOLUTEERROR:"",ONLINEINSVALUE:"",LABORATORYVALUE:null,RELATIVEERROR:null,EVARESULTE:"",REMARK:"",DETERTYPE:"1"};
					  if( $scope.SampleTableRowList.length==0){
						  $scope.SampleTableRowList.push(obj);  
					  } 
				 }
			});
		}
		/**
		 * 质控样品新增
		 */
		function sampleMeasureSave(){
			if($scope.param!='060'&&$scope.param!='101'&&$scope.param!='011'){
				$.messager.alert("提示","质控样品只做总磷、氨氮、COD的测定",'info');
				return;
			}
			if($scope.STASAMPLECON=="0"){
				$.messager.alert("提示","浓度值不能为0！",'info');
				return;
			}
			var count=0;
			for(var i=0;i<$scope.SampleTableRowList.length;i++){
				if( $scope.SampleTableRowList[i].SAMPLENUMBER!=""&&$scope.SampleTableRowList[i].SAMPLENUMBER!=null				
					&&$scope.SampleTableRowList[i].LABORATORYVALUE!=null&&$scope.SampleTableRowList[i].LABORATORYVALUE!=''
					&&$scope.SampleTableRowList[i].EVARESULTE!=null&&$scope.SampleTableRowList[i].EVARESULTE!=''){
					count++;
				var SAMPLENUMBER= $scope.SampleTableRowList[i].SAMPLENUMBER;
				if(SAMPLENUMBER!=undefined||SAMPLENUMBER!=null||SAMPLENUMBER!=""){
					if($scope.SampleTableRowList[i].EVARESULTE==0){
						$scope.NRESULT++;
					}
					$scope.SampleTableRowList[i].PARMNAME=$scope.param;
					$scope.SampleTableRowList[i].STASAMPLECON=$scope.STASAMPLECON;
					$scope.SampleTableRowList[i].STASAMPLEBATCHNUMBER=$scope.STASAMPLEBATCHNUMBER;
					$dataComparisonService.createComparisonDetailModel($scope.SampleTableRowList[i]).then(function(){
								
					});
				}else{
					$.messager.alert("提示","样品编号不能为空",'info');
					return;
				}
			}else{
				$.messager.alert("提示","数据不完整！",'info');
				return false;
			}
			}
			if(count==$scope.SampleTableRowList.length&&i>0){
				$.messager.alert("提示","保存成功！",'info');
				createModel();
			}		
		}
		/**
		 * 报告预览
		 */
		function reportPreview(){
			 $dataComparisonService.getComparisonList($scope.queryObject).then(function(data){
				 $scope.essential=data.data[0];
			 });
			 $scope.queryObject.DETERTYPE='0';
			 $dataComparisonService.getComparisonDetailList($scope.queryObject).then(function(data){
				 $scope.WaterTableRowList={};
				 if(data.data.length>0){
					 if($scope.param=='029'||$scope.param=='024'||$scope.param=='028'){
						 WaterMeasure();
					 }else{
						 $scope.WaterTableRowList=data.data;
					 }
				 }});	
			 $scope.queryObject.DETERTYPE='1';
			 $dataComparisonService.getComparisonDetailList($scope.queryObject).then(function(data){
				 $scope.Sampledata={};
				 if(data.data.length>0){												
					 $scope.Sampledata=data.data; 					
				 }});
			 $dataComparisonService.getComparisonList($scope.queryObject).then(function(data){
				 $scope.ScienceExplainData=data.data[0];
			 });	
		}
		function createModel(){
			$scope.DATACOMPARISONINFODATA={};
			$scope.DATACOMPARISONINFODATA.GUID=$scope.essential.GUID
			var nowDate = new Date();
			$scope.DATACOMPARISONINFODATA.ACCOMPLISHTIME=nowDate.getFullYear()+"-"+(nowDate.getMonth()+1)+"-"+nowDate.getDate()+" "+nowDate.getHours()+":"+nowDate.getMinutes()+":"+nowDate.getSeconds();
			$dataComparisonService.createComparisonModel($scope.DATACOMPARISONINFODATA).then(function(){				
				toggleDetail();
			});			
		}
		/**
		 * 时间验证
		 */
		$scope.checkErr = function(){		   
		    if((new Date($scope.essential.SAMPLEANABEGINTIME.replace(/-/g,"\/"))) > (new Date($scope.essential.SAMPLEANAENDTIME.replace(/-/g,"\/")))){
		    	$.messager.alert("提示","起始时间不能大于结束时间");
		      return false;
		    }	
		}
		/**
		 * 时间格式处理
		 */
		$scope.dateFormat=function(time){
			if(time<10){
				return "0"+time;
			}else{
				return time;
			}
		}
		/**
		 * 初始化
		 */
		function _init(){			
			$scope.detailData=$stateParams.curRow;
			$scope.parmData=[];
			$scope.queryObject.REPORTNUMBER=$scope.detailData.REPORTNUMBER;
			$dataComparisonService.getMoinitorParam( $scope.queryObject).then(function(data){				
				if(data.data.length>0){
					for(var i=0;i<data.data.length;i++){
						$scope.parm={};
						$scope.parm.k=data.data[i].S_DOMAINCODE;
						$scope.parm.v=data.data[i].S_DOMAINCAPTION;
						$scope.parmData.push($scope.parm);
					}
					$scope.param=data.data[0].S_DOMAINCODE;
				}	
				$scope.detailQuery=0;		
				toggleDetail();		
			});				
		}		
		_init();
		
	});
}(window, angular));	